package com.wpzf.zxhc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.utils.StringUtils;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.domain.*;
import com.wpzf.ndhc.mapper.WpSjsbNdDTOMapper;
import com.wpzf.ndhc.utils.GTYUtil;
import com.wpzf.system.api.model.LoginUser;
import com.wpzf.zxhc.mapper.WpzfDockMapper;
import com.wpzf.zxhc.service.WpzfDockService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.ModelMap;

import javax.crypto.Cipher;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class WpzfDockServiceImpl implements WpzfDockService {

    @Autowired
    TokenService tokenService;

    @Autowired
    private WpzfDockMapper wpzfDockMapper;

    @Autowired
    private WpSjsbNdDTOMapper wpSjsbNdDTOMapper;

    @Value("${exepath}")
    public String exePath;

    private final static String logUrl = "D:\\wpzf\\saveResultCreateDbLog.txt";

    ReentrantLock lock = new ReentrantLock();

    /**
     * RSA公钥
     */
    private static final String PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";

    /**
     * 广西加密密码
     *
     * @param password 原始密码
     * @return 加密后的密码
     */
    public static String encryptPassword(String password) {
        try {
            // 创建X509EncodedKeySpec对象
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(PUBLIC_KEY));
            // 创建KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            // 生成公钥
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            // 创建Cipher对象
            Cipher cipher = Cipher.getInstance("RSA");
            // 初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            // 加密
            byte[] result = cipher.doFinal(password.getBytes());
            // 返回加密后的结果
            return Base64.encodeBase64String(result);
        } catch (Exception e) {
            throw new RuntimeException("密码加密失败", e);
        }
    }

    private static String getGxToken() {
        ModelMap map = new ModelMap();
        String result = null;
        try {
            String phonenumber = "13269013075";
            String password = encryptPassword("zrzy@2024ST%!");
            String data = "{\"phonenumber\": \"" + phonenumber + "\",\"password\": \"" + password + "\"}";
            System.out.println(data);

            String postURL = "https://map.dnr.gxzf.gov.cn/zrzytcjc/loginPhoneApp";
            URL url = new URL(postURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
//            conn.setRequestProperty("accept", "*/*");
//            conn.setRequestProperty("connection", "Keep-Alive");
//            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setRequestProperty("Content-Type", "application/json");

            conn.setDoOutput(true);
            conn.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");

            out.write(data);
            //缓冲数据
            out.flush();
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            int count = 0;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            while ((count = is.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, count);
            }
            byte[] datas = byteArrayOutputStream.toByteArray();

            byteArrayOutputStream.close();
            //关闭流
            is.close();

            conn.disconnect();
            result = new String(datas, 0, datas.length, "utf-8");
        } catch (MalformedURLException ex) {
            throw new RuntimeException(ex);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        Map mapType = JSON.parseObject(result, Map.class);
        String token = mapType.get("token") + "";

        return token;
    }

    @Override
    public String putNdDkInfo(List<Map<String, Object>> param) {

        //获取举证照片
        List<Map<String, Object>> list = new ArrayList<>();
        for (Map<String, Object> dk : param) {
            ModelMap modelMap = getShowImgs(dk);
            list.add(modelMap);
        }
        Gson gson = new Gson();
        // 将List转换为JSON字符串
        return gson.toJson(list);
    }

    public ModelMap getShowImgs(Map<String, Object> parm) {
        ModelMap map = new ModelMap();
        try {
            String dkbh = parm.get("dkbh") + "";
            String xzqdm = parm.get("xzqdm") + "";
            String postURL = "";
            String data = "";
            {
                postURL = "https://jg.landcloud.org.cn:8553/webapi/token";
                data = "client_id=web&client_secret=e46232c20609435eba54b7576de935dd&grant_type=password&username=a53e0c14ee900728f36bba014d975982&password=b30c775c48e9ba92ade95e427a5242b0";
            }
            URL url = new URL(postURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

            conn.setDoOutput(true);
            conn.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");

            out.write(data);
            //缓冲数据
            out.flush();
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            int count = 0;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            while ((count = is.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, count);
            }
            byte[] datas = byteArrayOutputStream.toByteArray();

            byteArrayOutputStream.close();
            //关闭流
            is.close();

            conn.disconnect();
            String result = new String(datas, 0, datas.length, "utf-8");
            List<String> items = new ArrayList<>();
            {
//                String t = getImg(result, "00gty_wpzf_nddk", xzqdm, dkbh);
                String t = getImgNew(result, "00gty_wpzf_nddk", xzqdm, dkbh);
                items.add(t);
                map.put("data", items);
            }
        } catch (MalformedURLException ex) {
            throw new RuntimeException(ex);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        return map;
    }

    public String getImg(String str, String ywmb, String xzqdm, String dkbh) {
        String rest = "";
        try {
            String postURL = "";
            String token = "";
            String token_type = "";
            Map mapType = JSON.parseObject(str, Map.class);
            {
                for (Object obj : mapType.keySet()) {

                    if ((obj + "").equals("access_token")) {
                        token = mapType.get("access_token") + "";
                        token_type = mapType.get("token_type") + "";
                    }
                }
                System.out.println(token);
                //接口地址
                postURL = "https://dc2.landcloud.org.cn:9443/webapi/api/open/GetWPZFTBInfo";
            }
            URL url = new URL(postURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            String encoding = com.wpzf.common.core.utils.sign.Base64.encode(str.getBytes());
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Charset", "utf-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8 ");
            conn.setRequestProperty("Authorization", token_type + " " + token);
            //  conn.setRequestProperty("token", token);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(2000);
            conn.setReadTimeout(2000);
            //获取URLConnection对象对应的输出流
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            //  out = new PrintWriter(conn.getOutputStream());
            //发送请求参数即数据
            //String xmbh="210115S120240216LJ00005";
            if (StringUtils.isNotBlank(xzqdm)) {
                //String data = "{\"xzqdm\":\"" + xzqdm + "\",\"ywmb\":\"" + ywmb + "\"}";
                String data = "{\"xzqdm\":\"" + xzqdm + "\",\"ywmb\":\"" + ywmb + "\",\"xmbh\":[\"" + dkbh + "\"]}";
                System.out.println("---------获取地块列表参数------" + data);
                out.write(data);
                out.flush();
                InputStream is = conn.getInputStream();
                int count = 0;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[4096];
                while ((count = is.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, count);
                }
                byte[] datas = byteArrayOutputStream.toByteArray();

                byteArrayOutputStream.close();
                is.close();
                conn.disconnect();
                rest = new String(datas, 0, datas.length, "utf-8");
            }

        } catch (Exception e) {
            rest = "网络异常，请稍后重试！";
            //writeLog(UtilTool.GetTime_1()+"："+e.getMessage()+"\n");
            throw new RuntimeException(e.getMessage());
        }
        return rest;
    }

    public String getImgNew(String str, String ywmb, String xzqdm, String dkbh) {
        String rest = "";
        try {
            String postURL = "";
            String token = "";
            String token_type = "";
            Map mapType = JSON.parseObject(str, Map.class);
            {
                for (Object obj : mapType.keySet()) {

                    if ((obj + "").equals("access_token")) {
                        token = mapType.get("access_token") + "";
                        token_type = mapType.get("token_type") + "";
                    }
                }
                System.out.println(token);
                //接口地址
                postURL = "https://dc2.landcloud.org.cn:9443/webapi/getprojectCG/ndbg2024";
            }
            URL url = new URL(postURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            String encoding = com.wpzf.common.core.utils.sign.Base64.encode(str.getBytes());
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Charset", "utf-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8 ");
            conn.setRequestProperty("Authorization", token_type + " " + token);
            //  conn.setRequestProperty("token", token);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(20000);
            conn.setReadTimeout(20000);
            //获取URLConnection对象对应的输出流
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            //  out = new PrintWriter(conn.getOutputStream());
            //发送请求参数即数据
            //String xmbh="210115S120240216LJ00005";
            if (StringUtils.isNotBlank(xzqdm)) {
                String data = "{\"xzqdm\":\"" + xzqdm + "\",\"xmbh\":[\"" + dkbh + "\"]}";
                System.out.println("---------获取地块列表参数------" + data);
                out.write(data);
                out.flush();
                InputStream is = conn.getInputStream();
                int count = 0;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[4096];
                while ((count = is.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, count);
                }
                byte[] datas = byteArrayOutputStream.toByteArray();

                byteArrayOutputStream.close();
                is.close();
                conn.disconnect();
                rest = new String(datas, 0, datas.length, "utf-8");
            }

        } catch (Exception e) {
            rest = "网络异常，请稍后重试！";
            //writeLog(UtilTool.GetTime_1()+"："+e.getMessage()+"\n");
            throw new RuntimeException(e.getMessage());
        }
        return rest;
    }


    @Override
    @Transactional
    public String saveResult(Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        try {
            //记录
            Map<String, Object> map = new HashMap<>();
            JSONObject json = new JSONObject(param);
            map.put("data", json.toString());
            map.put("interface", "saveResult");
            map.put("user_name", loginUser.getUsername());
            map.put("nick_name", loginUser.getSysUser().getNickName());
            String user_xzqdm = loginUser.getSysUser().getXzqdm();
            map.put("xzqdm", user_xzqdm);
            map.put("submitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
            Integer rest = wpzfDockMapper.saveCallLog(map);

            if (rest > 0) {
                Integer saveTbCount = 0;
                Integer saveTbjbxxRest = 0;
                Integer saveTbdchsxxRest = 0;
                Integer dkjbxxRest = 0;
                Integer saveDkdchsxxRest = 0;
                Integer saveDkfjjbxxRest = 0;
                Integer saveDkpwjbxxRest = 0;
                Integer saveFjRest = 0;
                Integer saveDkshxxRest = 0;

                Set<String> xzqdmSet = new HashSet<>();

                //以整图斑为单位撤销
                List<Map<String, Object>> revokeTbList = (List<Map<String, Object>>) param.get("revokeTbList");
                Integer revokeTbCount = deleteTbList(revokeTbList, user_xzqdm);
                deleteQualitytesting(revokeTbList);
                if (revokeTbCount < 0) {
                    return "存储失败,数据省份与用户信息不符！";
                }

                //以图斑为单位保存
                List<Map<String, Object>> saveTbList = (List<Map<String, Object>>) param.get("saveTbList");
                if (saveTbList != null && saveTbList.size() > 0) {
//                    Integer i = deleteTbList(saveTbList, user_xzqdm);

                    saveTbCount = saveTbList.size();
                    Set<String> set = new HashSet<>();

                    for (Map<String, Object> saveTb : saveTbList) {

                        String stage = wpzfDockMapper.getStage(saveTb);

                        Integer i = deleteTb(saveTb, user_xzqdm);
                        if (i < 0) {
                            return "存储失败,数据省份与用户信息不符！";
                        }

//                    String tbbsm = saveTb.get("tbbsm") + "";
                        String xzqdm1 = saveTb.get("xzqdm") + "";
                        xzqdmSet.add(saveTb.get("xzqdm") + "");

                        Map<String, Object> tbInfo = (Map<String, Object>) saveTb.get("tbInfo");
                        if (tbInfo != null) {
                            //图斑基本信息表（TBJBXX）
                            Map<String, Object> tbjbxx = (Map<String, Object>) tbInfo.get("tbjbxx");

                            if (stage != null) {
                                tbjbxx.put("stage", stage);
                            } else {
                                tbjbxx.put("stage", tbjbxx.get("xzqdm") + "-1");
                            }

                            set.add(tbjbxx.get("stage") + "");

                            //todo 校验 BSM DKLX XZQDM XMC TBBH TBMJ SFBHZDK XZB YZB DKFW SJDRLY
                            if (tbjbxx.get("bsm") == null || "".equals(tbjbxx.get("bsm") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ")TBJBXX表bsm字段为必填项！";
                            }
                            String bsm = tbjbxx.get("bsm").toString();

                            if (tbjbxx.get("tbbh") == null || "".equals(tbjbxx.get("tbbh") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表tbbh字段为必填项！";
                            }
                            if (tbjbxx.get("dklx") == null || "".equals(tbjbxx.get("dklx") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表dklx字段为必填项！";
                            }

                            if (tbjbxx.get("xzqdm") == null || "".equals(tbjbxx.get("xzqdm") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表xzqdm字段为必填项！";
                            }

                            if (tbjbxx.get("xmc") == null || "".equals(tbjbxx.get("xmc") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表xmc字段为必填项！";
                            }


                            if (tbjbxx.get("tbmj") == null || "".equals(tbjbxx.get("tbmj") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表tbmj字段为必填项！";
                            }

                            if (tbjbxx.get("sfbhzdk") == null || "".equals(tbjbxx.get("sfbhzdk") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表sfbhzdk字段为必填项！";
                            }

                            if (tbjbxx.get("sfbhzdk") == null || "".equals(tbjbxx.get("sfbhzdk") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表sfbhzdk字段为必填项！";
                            }

                            if (tbjbxx.get("xzb") == null || "".equals(tbjbxx.get("xzb") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表xzb字段为必填项！";
                            }

                            if (tbjbxx.get("yzb") == null || "".equals(tbjbxx.get("yzb") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表yzb字段为必填项！";
                            }

                            if (tbjbxx.get("dkfw") == null || "".equals(tbjbxx.get("dkfw") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表dkfw字段为必填项！";
                            }

                            if (tbjbxx.get("sjdrly") == null || "".equals(tbjbxx.get("sjdrly") + "")) {
                                //回滚
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return "存储失败,(" + xzqdm1 + ",bsm为:" + bsm + ")TBJBXX表sjdrly字段为必填项！";
                            }

                            saveTbjbxxRest += wpzfDockMapper.saveTbjbxx(tbjbxx);

                            //图斑调查核实信息表（TBDCHSXX）
                            List<Map<String, Object>> tbdchsxxList = (List<Map<String, Object>>) tbInfo.get("tbdchsxxList");
                            if (tbdchsxxList != null && tbdchsxxList.size() > 0) {
                                for (Map<String, Object> tbdchsxx : tbdchsxxList) {
                                    //todo 校验 BSM TBBSM

                                    if (tbdchsxx.get("tbbsm") == null || "".equals(tbdchsxx.get("tbbsm") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败," + xzqdm1 + "TBDCHSXX表tbbsm字段为必填项！";
                                    }

                                    String tbbsm = tbdchsxx.get("tbbsm").toString();

                                    if (tbdchsxx.get("bsm") == null || "".equals(tbdchsxx.get("bsm") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,(" + xzqdm1 + "," + tbbsm + ")TBDCHSXX表bsm字段为必填项！";
                                    }

                                    saveTbdchsxxRest += wpzfDockMapper.saveTbdchsxx(tbdchsxx);
                                }
                            }
                        }


                        List<Map<String, Object>> dkDetails = (List<Map<String, Object>>) saveTb.get("dkDetails");
                        if (dkDetails != null && dkDetails.size() > 0) {
                            for (Map<String, Object> dkDetail : dkDetails) {
                                //图斑内所有地块
//                        String dkbsm = dkDetail.get("dkbsm") + "";
//                        String xzqdm = dkDetail.get("xzqdm") + "";
                                Map<String, Object> info = (Map<String, Object>) dkDetail.get("dkInfo");
                                if (info != null) {
                                    //地块基本信息表（DKJBXX）
                                    Map<String, Object> dkjbxx = (Map<String, Object>) info.get("dkjbxx");
                                    //todo 校验 BSM TBBSM XZQDM DKBH DKMJ (
                                    // SFBGJSYD SFGDWF SFNRHSQD GDMJ QTNYDMJ JSYDMJ WLYDMJ (
                                    // PDLX WF_WFLX1 WF_SJYT SFLZGDJF SFWHZJ SFDPF XMZT XMLX WF_SFLA
                                    // ))
                                    if (dkjbxx.get("bsm") == null || "".equals(dkjbxx.get("bsm") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,DKJBXX表bsm字段为必填项！";
                                    }
                                    String dkbsm = dkjbxx.get("bsm").toString();

                                    if (dkjbxx.get("tbbsm") == null || "".equals(dkjbxx.get("tbbsm") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表tbbsm字段为必填项！";
                                    }

                                    if (dkjbxx.get("xzqdm") == null || "".equals(dkjbxx.get("xzqdm") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表xzqdm字段为必填项！";
                                    }

                                    if (dkjbxx.get("dkbh") == null || "".equals(dkjbxx.get("dkbh") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表dkbh字段为必填项！";
                                    }

                                    if (dkjbxx.get("dkmj") == null || "".equals(dkjbxx.get("dkmj") + "")) {
                                        //回滚
                                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                        return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表dkmj字段为必填项！";
                                    }

                                    //kzxx
                                    if (("N".equals(dkjbxx.get("sfbgjsyd") + "") && "N".equals(dkjbxx.get("sfnrhsqd") + "")) || ("Y".equals(dkjbxx.get("sfbgjsyd") + "") && "N".equals(dkjbxx.get("sfgdwf") + "") && "N".equals(dkjbxx.get("sfnrhsqd") + ""))) {
                                        if (dkjbxx.get("gdmj") == null || "".equals(dkjbxx.get("gdmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表gdmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("qtnydmj") == null || "".equals(dkjbxx.get("qtnydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表qtnydmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("jsydmj") == null || "".equals(dkjbxx.get("jsydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表jsydmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("wlydmj") == null || "".equals(dkjbxx.get("wlydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表wlydmj字段为必填项！";
                                        }
                                    } else {
                                        if (dkjbxx.get("sfbgjsyd") == null || "".equals(dkjbxx.get("sfbgjsyd") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sfbgjsyd字段为必填项！";
                                        }

                                        if (dkjbxx.get("sfgdwf") == null || "".equals(dkjbxx.get("sfgdwf") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sfgdwf字段为必填项！";
                                        }

                                        if (dkjbxx.get("sfnrhsqd") == null || "".equals(dkjbxx.get("sfnrhsqd") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sfnrhsqd字段为必填项！";
                                        }

                                        if (dkjbxx.get("gdmj") == null || "".equals(dkjbxx.get("gdmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表gdmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("qtnydmj") == null || "".equals(dkjbxx.get("qtnydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表qtnydmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("jsydmj") == null || "".equals(dkjbxx.get("jsydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表jsydmj字段为必填项！";
                                        }

                                        if (dkjbxx.get("wlydmj") == null || "".equals(dkjbxx.get("wlydmj") + "")) {
                                            //回滚
                                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                            return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表wlydmj字段为必填项！";
                                        }

                                        //条件必填
                                        if ("Y".equals(dkjbxx.get("sfgdwf") + "") || "Y".equals(dkjbxx.get("sfnrhsqd") + "")) {
                                            if (dkjbxx.get("pdlx") == null || "".equals(dkjbxx.get("pdlx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表pdlx字段SFGDWF是否违法等于Y或SFNRHSQD是否纳入核实清单等于Y时必填！";
                                            }
                                        }

                                        if ("WFYD".equals(dkjbxx.get("pdlx") + "")) {
                                            if (dkjbxx.get("wf_wflx1") == null || "".equals(dkjbxx.get("wf_wflx1") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表wf_wflx1字段违法时必填！";
                                            }

                                            if (dkjbxx.get("wf_sjyt") == null || "".equals(dkjbxx.get("wf_sjyt") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表wf_sjyt字段违法时必填！";
                                            }

                                            if (dkjbxx.get("sflzgdjf") == null || "".equals(dkjbxx.get("sflzgdjf") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sflzgdjf字段违法时必填！";
                                            }

                                            if (dkjbxx.get("sfwhzj") == null || "".equals(dkjbxx.get("sfwhzj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sfwhzj字段违法时必填！";
                                            }

                                            if (dkjbxx.get("sfdpf") == null || "".equals(dkjbxx.get("sfdpf") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表sfdpf字段违法时必填！";
                                            }

                                            if (dkjbxx.get("xmzt") == null || "".equals(dkjbxx.get("xmzt") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表xmzt字段违法时必填！";
                                            }

                                            if (dkjbxx.get("xmlx") == null || "".equals(dkjbxx.get("xmlx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表xmlx字段违法时必填！";
                                            }

                                            if (dkjbxx.get("wf_sfla") == null || "".equals(dkjbxx.get("wf_sfla") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",bsm为:" + dkbsm + ")DKJBXX表wf_sfla字段违法时必填！";
                                            }
                                        }
                                    }


                                    dkjbxxRest += wpzfDockMapper.saveDkjbxx(dkjbxx);

                                    //地块调查核实信息表（DKDCHSXX）
                                    List<Map<String, Object>> dkdchsxxList = (List<Map<String, Object>>) info.get("dkdchsxxList");
                                    if (dkdchsxxList != null && dkdchsxxList.size() > 0) {
                                        for (Map<String, Object> dkdchsxx : dkdchsxxList) {
                                            //todo 校验 DKBSM
                                            if (dkdchsxx.get("dkbsm") == null || "".equals(dkdchsxx.get("dkbsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败," + xzqdm1 + "DKDCHSXX表dkbsm字段为必填项！";
                                            }
                                            saveDkdchsxxRest += wpzfDockMapper.saveDkdchsxx(dkdchsxx);
                                        }
                                    }
                                    //地块附件信息表（DKFJJBXX）
                                    List<Map<String, Object>> dkfjjbxxList = (List<Map<String, Object>>) info.get("dkfjjbxxList");
                                    if (dkfjjbxxList != null && dkfjjbxxList.size() > 0) {
                                        for (Map<String, Object> dkfjjbxx : dkfjjbxxList) {
                                            //todo 校验 BSM DKBSM XZQDM FJMC FJLX SCRY SCSJ FJDX MD5 URL

                                            if (dkfjjbxx.get("dkbsm") == null || "".equals(dkfjjbxx.get("dkbsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,DKFJJBXX表dkbsm字段为必填项！";
                                            }
                                            String dkbsm1 = dkfjjbxx.get("dkbsm").toString();

                                            if (dkfjjbxx.get("bsm") == null || "".equals(dkfjjbxx.get("bsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表bsm字段为必填项！";
                                            }


                                            if (dkfjjbxx.get("xzqdm") == null || "".equals(dkfjjbxx.get("xzqdm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表xzqdm字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("fjmc") == null || "".equals(dkfjjbxx.get("fjmc") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表fjmc字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("fjlx") == null || "".equals(dkfjjbxx.get("fjlx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表fjlx字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("scry") == null || "".equals(dkfjjbxx.get("scry") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表scry字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("scsj") == null || "".equals(dkfjjbxx.get("scsj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表scsj字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("fjdx") == null || "".equals(dkfjjbxx.get("fjdx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表fjdx字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("md5") == null || "".equals(dkfjjbxx.get("md5") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表md5字段为必填项！";
                                            }

                                            if (dkfjjbxx.get("url") == null || "".equals(dkfjjbxx.get("url") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm1 + ")DKFJJBXX表url字段为必填项！";
                                            }

                                            saveDkfjjbxxRest += wpzfDockMapper.saveDkfjjbxx(dkfjjbxx);
                                        }
                                    }
                                    //地块批文信息表（DKPWJBXX）
                                    List<Map<String, Object>> dkpwjbxxList = (List<Map<String, Object>>) info.get("dkpwjbxxList");
                                    if (dkpwjbxxList != null && dkpwjbxxList.size() > 0) {
                                        for (Map<String, Object> dkpwjbxx : dkpwjbxxList) {
                                            //todo 校验 BSM DKBSM PWLY PZMJ PZGDMJ PZNYDMJ PZYJJBNTMJ MD5
                                            if (dkpwjbxx.get("dkbsm") == null || "".equals(dkpwjbxx.get("dkbsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败," + xzqdm1 + "DKPWJBXX表dkbsm字段为必填项！";
                                            }
                                            String dkbsm2 = dkpwjbxx.get("dkbsm").toString();

                                            if (dkpwjbxx.get("bsm") == null || "".equals(dkpwjbxx.get("bsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表bsm字段为必填项！";
                                            }


                                            if (dkpwjbxx.get("pwly") == null || "".equals(dkpwjbxx.get("pwly") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表pwly字段为必填项！";
                                            }

                                            if (dkpwjbxx.get("pzmj") == null || "".equals(dkpwjbxx.get("pzmj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表pzmj字段为必填项！";
                                            }

                                            if (dkpwjbxx.get("pzgdmj") == null || "".equals(dkpwjbxx.get("pzgdmj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表pzgdmj字段为必填项！";
                                            }

                                            if (dkpwjbxx.get("pznydmj") == null || "".equals(dkpwjbxx.get("pznydmj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表pznydmj字段为必填项！";
                                            }

                                            if (dkpwjbxx.get("pzyjjbntmj") == null || "".equals(dkpwjbxx.get("pzyjjbntmj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表pzyjjbntmj字段为必填项！";
                                            }

                                            if (dkpwjbxx.get("md5") == null || "".equals(dkpwjbxx.get("md5") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm2 + ")DKPWJBXX表md5字段为必填项！";
                                            }

//                                String bsm = dkpwjbxx.get("bsm") + "";
//                                String dkbsm = dkpwjbxx.get("dkbsm") + "";
//                                Integer pwly = dkpwjbxx.get("pwly") + "";
//                                Integer pzmj = dkpwjbxx.get("pzmj") + "";
//                                Integer pzgdmj = dkpwjbxx.get("pzgdmj") + "";
//                                Integer pznydmj = dkpwjbxx.get("pznydmj") + "";
//                                Integer pzyjjbntmj = dkpwjbxx.get("pzyjjbntmj") + "";
//                                String md5 = dkpwjbxx.get("md5") + "";

                                            saveDkpwjbxxRest += wpzfDockMapper.saveDkpwjbxx(dkpwjbxx);
                                        }
                                    }
                                    //附件表（FJ）
                                    List<Map<String, Object>> fjList = (List<Map<String, Object>>) info.get("fjList");
                                    if (fjList != null && fjList.size() > 0) {
                                        for (Map<String, Object> fj : fjList) {
                                            //todo 校验 BSM DKLX DKBSM ZDKBH XZQDM FJLX FJHXZ PSSJ Longitude Latitude PSFYJ PSJD PSHGJ PSRY ZSDM JYM (
                                            // JDGD PSJJ FJYSKD FJYSGD SPKZXX PSDXZXX)
                                            if (fj.get("dkbsm") == null || "".equals(fj.get("dkbsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败," + xzqdm1 + "FJ表dkbsm字段为必填项！";
                                            }

                                            String dkbsm3 = fj.get("dkbsm").toString();

                                            if (fj.get("bsm") == null || "".equals(fj.get("bsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表bsm字段为必填项！";
                                            }

                                            if (fj.get("dklx") == null || "".equals(fj.get("dklx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表dklx字段为必填项！";
                                            }


                                            if (fj.get("zdkbh") == null || "".equals(fj.get("zdkbh") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表zdkbh字段为必填项！";
                                            }

                                            if (fj.get("xzqdm") == null || "".equals(fj.get("xzqdm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表xzqdm字段为必填项！";
                                            }

                                            if (fj.get("fjlx") == null || "".equals(fj.get("fjlx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表fjlx字段为必填项！";
                                            }

                                            if (fj.get("fjhxz") == null || "".equals(fj.get("fjhxz") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表fjhxz字段为必填项！";
                                            }

                                            if (fj.get("pssj") == null || "".equals(fj.get("pssj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表pssj字段为必填项！";
                                            }

                                            if (fj.get("longitude") == null || "".equals(fj.get("longitude") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表longitude字段为必填项！";
                                            }

                                            if (fj.get("latitude") == null || "".equals(fj.get("latitude") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表latitude字段为必填项！";
                                            }

                                            if (fj.get("psfyj") == null || "".equals(fj.get("psfyj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表psfyj字段为必填项！";
                                            }

                                            if (fj.get("psjd") == null || "".equals(fj.get("psjd") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表psjd字段为必填项！";
                                            }

                                            if (fj.get("pshgj") == null || "".equals(fj.get("pshgj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表pshgj字段为必填项！";
                                            }

                                            if (fj.get("psry") == null || "".equals(fj.get("psry") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表psry字段为必填项！";
                                            }

                                            if (!"4".equals(fj.get("fjlx") + "")) {
                                                if (fj.get("zsdm") == null || "".equals(fj.get("zsdm") + "")) {
                                                    //回滚
                                                    // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    //return "FJ表zsdm字段FJLX不为4为必填项！";
                                                }

                                                if (fj.get("jym") == null || "".equals(fj.get("jym") + "")) {
                                                    //回滚
                                                    //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    //return "FJ表jym字段FJLX不为4为必填项！";
                                                }
                                            }


                                            //条件必填
                                            if ("1".equals(fj.get("fjlx") + "")) {
                                                if (fj.get("jdgd") == null || "".equals(fj.get("jdgd") + "")) {
                                                    //回滚
                                                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表jdgd字段当为无人机拍摄时必填为必填项！";
                                                }

                                                if (fj.get("psjj") == null || "".equals(fj.get("psjj") + "")) {
                                                    //回滚
                                                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表psjj字段当为无人机拍摄时必填为必填项！";
                                                }

                                                if (fj.get("fjyskd") == null || "".equals(fj.get("fjyskd") + "")) {
                                                    //回滚
                                                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表fjyskd字段当为无人机拍摄时必填为必填项！";
                                                }

                                                if (fj.get("fjysgd") == null || "".equals(fj.get("fjysgd") + "")) {
                                                    //回滚
                                                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表fjysgd字段当为无人机拍摄时必填为必填项！";
                                                }
                                            }

                                            if ("4".equals(fj.get("fjlx") + "")) {
                                                if (fj.get("spkzxx") == null || "".equals(fj.get("spkzxx") + "")) {
                                                    //回滚
                                                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                    return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm3 + ")FJ表spkzxx字段当为手机视频时必填为必填项！";
                                                }

                                            }

                                            //条件必填
//                                String psdxzxx = fj.get("psdxzxx") + "";

                                            saveFjRest += wpzfDockMapper.saveFj(fj);
                                        }
                                    }
                                    //地块审核信息表（DKSHXX）
                                    List<Map<String, Object>> dkshxxList = (List<Map<String, Object>>) info.get("dkshxxList");
                                    if (dkshxxList != null && dkshxxList.size() > 0) {
                                        for (Map<String, Object> dkshxx : dkshxxList) {
                                            //todo 校验 BSM DKBSM DKLX XZQDM SHJD SHRY SHJG SHSJ
                                            if (dkshxx.get("dkbsm") == null || "".equals(dkshxx.get("dkbsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败," + xzqdm1 + "DKSHXX表dkbsm字段为必填项！";
                                            }
                                            String dkbsm4 = dkshxx.get("dkbsm").toString();

                                            if (dkshxx.get("bsm") == null || "".equals(dkshxx.get("bsm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表bsm字段为必填项！";
                                            }


                                            if (dkshxx.get("dklx") == null || "".equals(dkshxx.get("dklx") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表dklx字段为必填项！";
                                            }

                                            if (dkshxx.get("xzqdm") == null || "".equals(dkshxx.get("xzqdm") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表xzqdm字段为必填项！";
                                            }

                                            if (dkshxx.get("shjd") == null || "".equals(dkshxx.get("shjd") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表shjd字段为必填项！";
                                            }

                                            if (dkshxx.get("shry") == null || "".equals(dkshxx.get("shry") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表shry字段为必填项！";
                                            }

                                            if (dkshxx.get("shjg") == null || "".equals(dkshxx.get("shjg") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表shjg字段为必填项！";
                                            }

                                            if (dkshxx.get("shsj") == null || "".equals(dkshxx.get("shsj") + "")) {
                                                //回滚
                                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                                return "存储失败,(" + xzqdm1 + ",dkbsm为:" + dkbsm4 + ")DKSHXX表shsj字段为必填项！";
                                            }

                                            saveDkshxxRest += wpzfDockMapper.saveDkshxx(dkshxx);
                                        }
                                    }
                                    //加密信息表（JMXX）
//        List<Map<String,Object>> jmxxList = (List<Map<String,Object>>)detail.get("jmxxList");
                                    //版本信息表（DB_INFO）
//        List<Map<String,Object>> dbinfoList = (List<Map<String,Object>>)detail.get("dbinfoList");
                                }
                            }
                        }
                    }

                    //修改状态 wp_sjsb_qualitytesting_nd_2024
                    if (set != null && set.size() > 0) {
                        for (String xzqdm : set) {
                            Map<String, Object> m = new HashMap<>();
                            m.put("xzqdm", xzqdm);
                            Map<String, Object> mm = wpzfDockMapper.queryQualitytestingByXzqdm(m);
                            if (mm != null) {
                                wpzfDockMapper.updateQualitytestingUpdateStatus(m);
                            } else {
                                wpzfDockMapper.saveQualitytesting(m);
                            }
                        }
                    }
                }
                return "撤销图斑共：" + revokeTbCount + "条，保存图斑共：" + saveTbCount + "条。";
            } else {
                return "存储失败";
            }
        } catch (Exception exception) {
            System.out.println(exception.toString());
            return "存储失败";
        }
    }

    @Override
    @Transactional
    public String deleteResult(Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        //记录
        Map<String, Object> map = new HashMap<>();
        JSONObject json = new JSONObject(param);
        map.put("data", json.toString());
        map.put("interface", "deleteResult");
        map.put("user_name", loginUser.getUsername());
        map.put("nick_name", loginUser.getSysUser().getNickName());
        map.put("xzqdm", loginUser.getSysUser().getXzqdm());
        map.put("submitdate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime()));
        Integer rest = wpzfDockMapper.saveCallLog(map);

        if (rest > 0) {
            //以整图斑为单位撤销
            List<Map<String, Object>> revokeTbList = (List<Map<String, Object>>) param.get("revokeTbList");
            Integer revokeTbCount = deleteTbList(revokeTbList, loginUser.getSysUser().getXzqdm());
            deleteQualitytesting(revokeTbList);
            if (revokeTbCount < 0) {
                return "数据省份与用户信息不符！";
            }
            return "撤销图斑共：" + revokeTbCount;
        } else {
            return "失败";
        }
    }

    private void deleteQualitytesting(List<Map<String, Object>> revokeTbList) {
        if (revokeTbList != null && revokeTbList.size() > 0) {
            Set<String> set = new HashSet<>();
            for (Map<String, Object> revokeTb : revokeTbList) {
                set.add(wpzfDockMapper.getStage(revokeTb));
            }
            if (set != null && set.size() > 0) {
                for (String xzqdm : set) {
                    List<Map<String, Object>> list = wpzfDockMapper.querySjsbTbjbxxByXzqdm(xzqdm);
                    if (list == null) {
                        wpzfDockMapper.deleteQualitytesting(xzqdm);
                    } else {
                        Map<String, Object> m = new HashMap<>();
                        m.put("xzqdm", xzqdm);
                        wpzfDockMapper.updateQualitytestingUpdateStatus(m);
                    }
                }
            }
        }
    }

    //以整图斑为单位撤销
    private Integer deleteTbList(List<Map<String, Object>> params, String user_xzqdm) {
        Integer revokeTbCount = 0;
        Integer deleteTbjbxxRest = 0;
        Integer deleteTbdchsxxRest = 0;
        Integer deleteDkjbxxRest = 0;
        Integer deleteDkdchsxxRest = 0;
        Integer deleteDkfjjbxxRest = 0;
        Integer deleteDkpwjbxxRest = 0;
        Integer deleteFjRest = 0;
        Integer deleteDkshxxRest = 0;

        if (params != null && params.size() > 0) {
            for (Map<String, Object> revokeTb : params) {
                //根据tbbsm查询删除
                String tbbsm = revokeTb.get("tbbsm") + "";
                String xzqdm = revokeTb.get("xzqdm") + "";
                revokeTb.put("user_xzqdm", user_xzqdm);
                if (user_xzqdm.equals(xzqdm.substring(0, 2))) {
                    //清除上报图斑表 图斑基本信息表（TBJBXX） 图斑调查核实信息表（TBDCHSXX）
                    revokeTbCount = wpzfDockMapper.deleteTbjbxx(revokeTb);
                    if (revokeTbCount > 0) {
                        deleteTbjbxxRest += revokeTbCount;
                        deleteTbdchsxxRest += wpzfDockMapper.deleteTbdchsxx(revokeTb);

                        //根据图斑查dkbsm 地块基本信息表（DKJBXX）
                        List<Map<String, Object>> dkjbxxList = wpzfDockMapper.queryDkjbxxByTbbsm(revokeTb);
                        deleteDkjbxxRest += wpzfDockMapper.deleteDkjbxx(revokeTb);
                        if (dkjbxxList != null && dkjbxxList.size() > 0) {
                            for (Map<String, Object> dkjbxx : dkjbxxList) {

                                //地块调查核实信息表（DKDCHSXX）
                                deleteDkdchsxxRest += wpzfDockMapper.deleteDkdchsxx(dkjbxx);
                                //地块附件信息表（DKFJJBXX）
                                deleteDkfjjbxxRest += wpzfDockMapper.deleteDkfjjbxx(dkjbxx);
                                //地块批文信息表（DKPWJBXX）
                                deleteDkpwjbxxRest += wpzfDockMapper.deleteDkpwjbxx(dkjbxx);
                                //附件表（FJ）
                                deleteFjRest += wpzfDockMapper.deleteFj(dkjbxx);
                                //地块审核信息表（DKSHXX）
                                deleteDkshxxRest += wpzfDockMapper.deleteDkshxx(dkjbxx);
                            }
                        }
                    }
                } else {
                    return -1;
                }
            }
        }
        return revokeTbCount;
    }

    //以整图斑为单位撤销
    private Integer deleteTb(Map<String, Object> param, String user_xzqdm) {
        Integer revokeTbCount = 0;
        Integer deleteTbjbxxRest = 0;
        Integer deleteTbdchsxxRest = 0;
        Integer deleteDkjbxxRest = 0;
        Integer deleteDkdchsxxRest = 0;
        Integer deleteDkfjjbxxRest = 0;
        Integer deleteDkpwjbxxRest = 0;
        Integer deleteFjRest = 0;
        Integer deleteDkshxxRest = 0;

        //根据tbbsm查询删除
        String tbbsm = param.get("tbbsm") + "";
        String xzqdm = param.get("xzqdm") + "";
        param.put("user_xzqdm", user_xzqdm);
        if (user_xzqdm.equals(xzqdm.substring(0, 2))) {
            //清除上报图斑表 图斑基本信息表（TBJBXX） 图斑调查核实信息表（TBDCHSXX）
            revokeTbCount = wpzfDockMapper.deleteTbjbxx(param);
            if (revokeTbCount > 0) {
                deleteTbjbxxRest += revokeTbCount;
                deleteTbdchsxxRest += wpzfDockMapper.deleteTbdchsxx(param);

                //根据图斑查dkbsm 地块基本信息表（DKJBXX）
                List<Map<String, Object>> dkjbxxList = wpzfDockMapper.queryDkjbxxByTbbsm(param);
                deleteDkjbxxRest += wpzfDockMapper.deleteDkjbxx(param);
                if (dkjbxxList != null && dkjbxxList.size() > 0) {
                    for (Map<String, Object> dkjbxx : dkjbxxList) {

                        //地块调查核实信息表（DKDCHSXX）
                        deleteDkdchsxxRest += wpzfDockMapper.deleteDkdchsxx(dkjbxx);
                        //地块附件信息表（DKFJJBXX）
                        deleteDkfjjbxxRest += wpzfDockMapper.deleteDkfjjbxx(dkjbxx);
                        //地块批文信息表（DKPWJBXX）
                        deleteDkpwjbxxRest += wpzfDockMapper.deleteDkpwjbxx(dkjbxx);
                        //附件表（FJ）
                        deleteFjRest += wpzfDockMapper.deleteFj(dkjbxx);
                        //地块审核信息表（DKSHXX）
                        deleteDkshxxRest += wpzfDockMapper.deleteDkshxx(dkjbxx);
                    }
                }
            }
        } else {
            return -1;
        }
        return revokeTbCount;
    }

    @Override
    public List<Map<String, Object>> queryResult(Map<String, Object> param) {
        //获取所有监测编号
        List<WpSjsbTbjbxxNdDTO> tbjbxxList = wpzfDockMapper.queryTbjbxxList(param);

        List<Map<String, Object>> rest = new ArrayList<>();

        if (tbjbxxList != null && tbjbxxList.size() > 0) {
            for (WpSjsbTbjbxxNdDTO tbjbxx : tbjbxxList) {
                Map<String, Object> tb = new HashMap<>();
                tb.put("tbbsm", tbjbxx.getBsm());
                tb.put("tbbh", tbjbxx.getTbbh());
                tb.put("xzqdm", tbjbxx.getXzqdm());
                Map<String, Object> tbInfo = new HashMap<>();
                tbInfo.put("tbjbxx", tbjbxx);
                List<WpSjsbTbdchsxxNdDTO> tbdchsxxList = wpzfDockMapper.queryTbdchsxxList(tb);
                tbInfo.put("tbdchsxxList", tbdchsxxList);
                tb.put("tbInfo", tbInfo);

                List<Map<String, Object>> dkList = new ArrayList<>();

                List<WpSjsbDkjbxxNdDTO> dkjbxxList = wpzfDockMapper.queryDkjbxxList(tb);
                for (WpSjsbDkjbxxNdDTO wpSjsbDkjbxxNdDTO : dkjbxxList) {
                    Map<String, Object> dk = new HashMap<>();
                    dk.put("dkbsm", wpSjsbDkjbxxNdDTO.getBsm());
                    dk.put("dkbh", wpSjsbDkjbxxNdDTO.getDkbh());
                    dk.put("xzqdm", wpSjsbDkjbxxNdDTO.getXzqdm());
                    Map<String, Object> dkInfo = new HashMap<>();
                    dkInfo.put("dkjbxx", wpSjsbDkjbxxNdDTO);
                    List<WpSjsbDkdchssxNdDTO> dkdchsxxList = wpzfDockMapper.queryDkdchsxxList(dk);
                    dkInfo.put("dkdchsxxList", dkdchsxxList);
                    List<WpSjsbDkfjjbxxNdDTO> dkfjjbxxList = wpzfDockMapper.queryDkfjjbxxList(dk);
                    dkInfo.put("dkfjjbxxList", dkfjjbxxList);
                    List<WpSjsbDkpwjbbxxNdDTO> dkpwjbxxList = wpzfDockMapper.queryDkpwjbxxList(dk);
                    dkInfo.put("dkpwjbxxList", dkpwjbxxList);
                    List<WpSjsbFjNdDTO> fjList = wpzfDockMapper.queryFjList(dk);
                    dkInfo.put("fjList", fjList);
                    List<WpSjsbDkshxxNdDTO> dkshxxList = wpzfDockMapper.queryDkshxxList(dk);
                    dkInfo.put("dkshxxList", dkshxxList);
                    dk.put("dkInfo", dkInfo);
                    dkList.add(dk);
                }
                tb.put("dkDetails", dkList);
                rest.add(tb);
            }
        }
        return rest;
    }

    @Override
    public Integer queryResultCount(Map<String, Object> param) {
        return wpzfDockMapper.queryTbjbxxListCount(param);
    }

    @Override
    public List<Map<String, Object>> queryQualityTestingResult(Map<String, Object> param) {
        return wpzfDockMapper.queryQualityTestingResult(param);
    }

    @Override
    public Integer queryQualityTestingResultCount(Map<String, Object> param) {
        return wpzfDockMapper.queryQualityTestingResultCount(param);
    }

    @Override
    /*public List<Map<String, Object>> getDB(Map<String, Object> param) {
        List<Map<String, Object>> rest = new ArrayList<>();

        List<String> xzqdmList = wpzfDockMapper.queryXzqdmToSjsbTbjbxx(param);

        //根据数据内所有行政区代码打包db
        DBUtil dbUtil = new DBUtil();
        for (String xzqdm : xzqdmList) {
            Map<String, Object> map = new HashMap<>();

            Map<String, Object> dbresult = dbUtil.getDBCreate(wpSjsbNdDTOMapper, xzqdm, "0");

            String dburl = "";
            String dbpassword = "";
            String message = "";
            String createtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((new Date()).getTime());

            if (dbresult.get("code").equals(200)) {
                //02 DB创建成功 进行质检
                String dbPath = dbresult.get("dbpath").toString();
                String dbPass = dbresult.get("dbpass").toString();

                dbpassword = dbPass;

                String dbOBSAddress = "getDB/" + xzqdm + ".db";
                AtomicInteger attemptCount1 = new AtomicInteger(0);
                String dbOBSlog;
                do {
                    dbOBSlog = OBSUploadUnits.OBSUpload(dbOBSAddress, dbPath);
                    attemptCount1.incrementAndGet();
                } while (!dbOBSlog.equals("success") && attemptCount1.get() < 5);
                dburl = "https://obs-top.obs.cn-north-1.myhuaweicloud.com/" + dbOBSAddress;

            } else {
                //DB创建失败
                message = "DB创建失败," + dbresult.get("message").toString();
            }

            map.put("xzqdm", xzqdm);
            map.put("dburl", dburl);
            map.put("message", message);
            map.put("dbpassword", dbpassword);
            map.put("createtime", createtime);

            rest.add(map);
        }
        return rest;
    }*/ public List<Map<String, Object>> getDB(Map<String, Object> param) {
        return wpzfDockMapper.getDBInfo(param);
    }

    @Override
    public List<Map<String, Object>> queryQualitytesting(Map<String, Object> param) {
        return wpzfDockMapper.queryQualitytesting(param);
    }

    @Override
    public Map<String, Object> queryQualitytestingByXzqdm(Map<String, Object> param) {
        return wpzfDockMapper.queryQualitytestingByXzqdm(param);
    }

    @Override
    public Integer updateQualitytestingByStatus(Map<String, Object> param) {
        return wpzfDockMapper.updateQualitytestingByStatus(param);
    }

    @Override
    public Integer updateQualitytestingBySubmitStatus(Map<String, Object> param) {
        return wpzfDockMapper.updateQualitytestingBySubmitStatus(param);
    }

    @Override
    public Integer updateQualitytestingByInfo(Map<String, Object> param) {
        return wpzfDockMapper.updateQualitytestingByInfo(param);
    }

    @Override
    public List<Map<String, Object>> queryAllQualitytestingByXzqdm(Map<String, Object> param) {
        return wpzfDockMapper.queryAllQualitytestingByXzqdm(param);
    }

    public Integer updateQualitytestingToJobid(Map<String, Object> param) {
        return wpzfDockMapper.updateQualitytestingToJobid(param);
    }

    @Override
    public Map<String, Object> queryDataAnalysis(Map<String, Object> param) {
        Map<String, Object> rest = null;

        Map<String, Object> qualitytesting = wpzfDockMapper.queryQualitytestingByXzqdm(param);
        if (qualitytesting != null && qualitytesting.get("jobid") != null) {
            rest = GTYUtil.queryDataPackageState(qualitytesting.get("jobid") + "");
        }
        return rest;
    }

    @Override
    public Map<String, Object> pushDB(Map<String, Object> param) {


        return Collections.emptyMap();
    }

    @Override
    public List<Map<String, Object>> queryDataAnalysisList(Map<String, Object> param) {
        List<Map<String, Object>> rest = new ArrayList<>();
        List<Map<String, Object>> list = wpzfDockMapper.queryXzqdmToQueryDataAnalysis(param);
        for (Map<String, Object> map : list) {
            Map<String, Object> m = queryDataAnalysis(map);
            if (m != null) {
                rest.add(m);
            }
        }
        return rest;
    }
}
